﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Resources;
using QualityStormCommon;

namespace QualityStorm {
    class DataGridViewTriStateCell : DataGridViewImageCell {
        Permission m_Value = Permission.None;
        Image none;
        Image deny;
        Image grant;
        public DataGridViewTriStateCell()
            : base() {
            Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            none = Image.FromStream(assembly.GetManifestResourceStream("QualityStorm.none.bmp"));
            deny = Image.FromStream(assembly.GetManifestResourceStream("QualityStorm.deny.bmp"));
            grant = Image.FromStream(assembly.GetManifestResourceStream("QualityStorm.grant.bmp"));

        }
        public Permission StateValue {
            get {
                return m_Value;
            }
            set {
                if (value == Permission.None) {
                    this.Value = none;
                } else if (value == Permission.Grant) {
                    this.Value = grant;
                } else if (value == Permission.Deny) {
                    this.Value = deny;
                }
                if (this.DataGridView != null) {
                    this.DataGridView.NotifyCurrentCellDirty(true);
                }

                m_Value = value;

            }

        }
        public override Type EditType {
            get {
                return null;
            }
        }

        public override object Clone() {
            DataGridViewTriStateCell tristateCell = (DataGridViewTriStateCell)base.Clone();
            if (tristateCell != null) {
                tristateCell.Value = StateValue;
            }

            return tristateCell;
        }
        protected override bool ContentClickUnsharesRow(DataGridViewCellEventArgs e) {
            Point ptCurrentCell = DataGridView.CurrentCellAddress;
            return ptCurrentCell.X == ColumnIndex && ptCurrentCell.Y == e.RowIndex;
        }

        protected override void OnContentClick(DataGridViewCellEventArgs e) {
            if (DataGridView == null) {
                return;
            }

            Point ptCurrentCell = DataGridView.CurrentCellAddress;
            if (ptCurrentCell.X == this.ColumnIndex &&
                ptCurrentCell.Y == e.RowIndex) {
                if (StateValue == Permission.None) {
                    StateValue = Permission.Grant;

                } else if (StateValue == Permission.Grant) {
                    StateValue = Permission.Deny;

                } else if (StateValue == Permission.Deny) {
                    StateValue = Permission.None;

                }

            }
        }
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) {
            return new Size(16, 16);
        }
    }
}